use TestHelper;
use EntityBag;
use EntityTest;

sub testClass{
    my($obj, $type) = (shift, shift);
    print "\n";
    print "Comprobando tipo: $type\n";
    if( (ref $obj) ne $type){
	print "    Tipo del objeto no valido: ".(ref $obj)."\n";
    }else{
	print "    Tipo correcto\n";
    }
}

sub testCategory{
    my($bag, $entity, $category) = (shift, shift, shift);
    print "\n";
    print "Comprobando categoria\n";


    @cat = @{$bag->category($category)};
    if(scalar(@cat) != 1){
	print "--->ERROR: Categoria ".$category." tiene tamaño erroneo: ".(scalar(@cat))."\n";
    }else{
	print "    Size OK\n";
    }
    
    $fromBag = $cat[0];
    
    if($entity ne $fromBag){
	print "--->ERROR: Los objetos son distintos: ".$entity." ne ".$fromBag."\n";
    }else{
	print "    Identidad OK\n";
    }
    
}

sub testEmptyCategory{
    my($bag, $category) = (shift, shift);
    print "\n";
    print "Comprobando categoria vacia\n";
    @cat = @{$bag->category($category)};
    if(scalar(@cat) != 0){
	print "--->ERROR: Categoria ".$category." tiene tamaño erroneo: ".(scalar(@cat))."\n";
    }else{
	print "    Size OK\n";
    }
}

sub testWhere{
    my ($bag, $entity, $category) = (shift, shift, shift);
    
    print "Probando WHERE en $category\n";
    
    if($bag->where($entity) ne $category){
	print "--->ERROR: Entidad no esta en $category; Found -".($bag->where($entity))."-\n";
    }else{
	print "    Where OK\n";
    }
}# testWhere

sub testAll{
    my ($bag, $entity) = (shift, shift);

    print "Probando contenido total \n";

    my @all = @{$bag->all};
    my $allerr = 1;
    foreach my $e (@all){
	if($e eq $entity){
	    $allerr = 0;
	    print "    Existe OK\n";
	}
    }
    if($allerr){
	print "--->ERROR: No se encontro en el contenido total\n";
    }


} # testAll


sub testRemove{
    my ($bag, $entity, $category) = (shift, shift, shift);

    print "Probando eliminacion\n";
    
    $bag->remove($entity);

    if($bag->where($entity) ne ""){
	print "--->ERROR: Entidad sigue existiendo en la categoria ".$bag->where($entity)."\n"; 
    }else{
	print "    Eliminacion de la categoria OK\n";
    }
    
    my @all = $bag->all;
    my $allerr = 0;
    foreach my $e (@all){
	if($e eq $entity){
	    $allerr = 1;
	    print "--->ERROR: aun existe: ".$e." - ".$entity."\n";
	}
    }
    if(!$allerr){
	print "    Eliminacion total OK\n";
    }
} # testRemove

1;
